package com.shuda.business.monitor.controller;

import com.shuda.business.monitor.dto.DataMonitorDTO;
import com.shuda.business.monitor.dto.MonitorStatisticsDTO;
import com.shuda.business.monitor.service.DataMonitorService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.result.Result;
import com.shuda.common.utils.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 数据监控控制器类
 */
@RestController
@RequestMapping("/api/data-monitor")
@Tag(name = "数据监控", description = "数据监控相关接口")
@RequiredArgsConstructor
@Validated
public class DataMonitorController {

    private final DataMonitorService dataMonitorService;

    /**
     * 获取监控统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取监控统计信息")
    public Result<MonitorStatisticsDTO> getMonitorStatistics(@RequestParam(required = false) String channelId,
                                                           @RequestParam(required = false) String schemeId,
                                                           @RequestParam(required = false) String timeRange) {
        MonitorStatisticsDTO statistics = dataMonitorService.getMonitorStatistics(channelId, schemeId, timeRange);
        return Result.success(statistics);
    }

    /**
     * 分页查询监控数据列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询监控数据列表")
    public Result<PageResult<DataMonitorDTO>> getMonitorDataPage(@Valid PageDTO pageDTO,
                                                                 @RequestParam(required = false) String channelId,
                                                                 @RequestParam(required = false) String schemeId,
                                                                 @RequestParam(required = false) String status,
                                                                 @RequestParam(required = false) String startTime,
                                                                 @RequestParam(required = false) String endTime) {
        PageResult<DataMonitorDTO> pageResult = dataMonitorService.getMonitorDataPage(pageDTO, channelId, schemeId, status, startTime, endTime);
        return Result.success(pageResult);
    }

    /**
     * 获取实时监控数据
     */
    @GetMapping("/realtime")
    @Operation(summary = "获取实时监控数据")
    public Result<Map<String, Object>> getRealtimeMonitorData(@RequestParam(required = false) String channelId,
                                                            @RequestParam(required = false) String schemeId) {
        Map<String, Object> realtimeData = dataMonitorService.getRealtimeMonitorData(channelId, schemeId);
        return Result.success(realtimeData);
    }

    /**
     * 获取渠道分发统计
     */
    @GetMapping("/channel-distribution")
    @Operation(summary = "获取渠道分发统计")
    public Result<List<Map<String, Object>>> getChannelDistributionStatistics(@RequestParam(required = false) String timeRange) {
        List<Map<String, Object>> statistics = dataMonitorService.getChannelDistributionStatistics(timeRange);
        return Result.success(statistics);
    }

    /**
     * 获取数据处理成功率统计
     */
    @GetMapping("/processing-success-rate")
    @Operation(summary = "获取数据处理成功率统计")
    public Result<Map<String, Object>> getProcessingSuccessRate(@RequestParam(required = false) String timeRange) {
        Map<String, Object> successRate = dataMonitorService.getProcessingSuccessRate(timeRange);
        return Result.success(successRate);
    }

    /**
     * 获取异常数据统计
     */
    @GetMapping("/exception-statistics")
    @Operation(summary = "获取异常数据统计")
    public Result<Map<String, Object>> getExceptionStatistics(@RequestParam(required = false) String timeRange) {
        Map<String, Object> exceptionStats = dataMonitorService.getExceptionStatistics(timeRange);
        return Result.success(exceptionStats);
    }

    /**
     * 获取性能监控数据
     */
    @GetMapping("/performance")
    @Operation(summary = "获取性能监控数据")
    public Result<Map<String, Object>> getPerformanceMonitorData(@RequestParam(required = false) String timeRange) {
        Map<String, Object> performanceData = dataMonitorService.getPerformanceMonitorData(timeRange);
        return Result.success(performanceData);
    }

    /**
     * 获取数据质量报告
     */
    @GetMapping("/quality-report")
    @Operation(summary = "获取数据质量报告")
    public Result<Map<String, Object>> getDataQualityReport(@RequestParam(required = false) String timeRange) {
        Map<String, Object> qualityReport = dataMonitorService.getDataQualityReport(timeRange);
        return Result.success(qualityReport);
    }

    /**
     * 导出监控数据
     */
    @GetMapping("/export")
    @Operation(summary = "导出监控数据")
    public Result<String> exportMonitorData(@RequestParam(required = false) String channelId,
                                          @RequestParam(required = false) String schemeId,
                                          @RequestParam(required = false) String startTime,
                                          @RequestParam(required = false) String endTime,
                                          @RequestParam(required = false) String format) {
        String filePath = dataMonitorService.exportMonitorData(channelId, schemeId, startTime, endTime, format);
        return Result.success(filePath);
    }

    /**
     * 获取监控告警配置
     */
    @GetMapping("/alert-config")
    @Operation(summary = "获取监控告警配置")
    public Result<Map<String, Object>> getAlertConfig() {
        Map<String, Object> alertConfig = dataMonitorService.getAlertConfig();
        return Result.success(alertConfig);
    }

    /**
     * 更新监控告警配置
     */
    @PutMapping("/alert-config")
    @Operation(summary = "更新监控告警配置")
    public Result<String> updateAlertConfig(@RequestBody Map<String, Object> config) {
        dataMonitorService.updateAlertConfig(config);
        return Result.success("监控告警配置更新成功");
    }
}